package com.newtouch.bxzs.database.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.newtouch.bxzs.common.base.model.CooperationCompanyOrganization;
import com.newtouch.bxzs.database.model.CooperationCompanyOrganizationBO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * 同步保险/银行机构表
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020/12/23 9:18</td>
 * <td>lhx</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lhx
 * @version 1.0.0
 * @since 1.0.0
 */
@Mapper
public interface CooperationCompanyOrganizationMapper extends BaseMapper<CooperationCompanyOrganization> {

    @Select(value = "select getOppisiteParentLst(#{cooperationCompanyOrganizationId},#{dataSource})")
    String modifyOrganizationMapping(@Param("cooperationCompanyOrganizationId")String cooperationCompanyOrganizationId,@Param("dataSource")String dataSource);

    @Select({ "<script>SELECT a.COOPERATION_COMPANY_ORGANIZATION_ID,a.COOPERATION_COMPANY_ORGANIZATION_NAME,a.COOPERATION_COMPANY_ORGANIZATION_CODE,a.COOPERATION_COMPANY_ORGANIZATION_TELEPHONE,a.IS_ENABLE as enableFlag,a.CREATE_TIME, " +
            "CASE WHEN b.HAVE_CHAILD IS NOT NULL THEN " +
            "1 ELSE 0 " +
            "END AS HAVE_CHAILD FROM ( SELECT t.* FROM ASSISTANT_COOPERATION_COMPANY_ORGANIZATION t <where> " +
            "<choose> " +
            "<when test='code !=null and code != \"\"'> " +
            "AND t.COOPERATION_COMPANY_PARENT_ORGANIZATION_CODE = #{code, jdbcType=VARCHAR} " +
            "</when> " +
            "<otherwise> " +
            "AND t.COOPERATION_COMPANY_PARENT_ORGANIZATION_CODE is null " +
            "</otherwise> " +
            "</choose>" +
            "</where>) a LEFT JOIN ( SELECT DISTINCT t1.COOPERATION_COMPANY_PARENT_ORGANIZATION_CODE HAVE_CHAILD FROM ASSISTANT_COOPERATION_COMPANY_ORGANIZATION t1 ) b ON a.COOPERATION_COMPANY_ORGANIZATION_CODE = b.HAVE_CHAILD " +
            "ORDER BY a.COOPERATION_COMPANY_ORGANIZATION_CODE " +
            "</script>" })
    List<CooperationCompanyOrganizationBO> CooperationCompanyOrganizationByChild(@Param("code") String code);
}
